package com.huilian.iotbox.client.controller;

import com.huilian.iotbox.data.properties.ConfigProperties;
import com.huilian.iotbox.client.security.jwt.util.JwtTokenUtil;
import com.huilian.iotbox.client.service.DeviceService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.dto.MemberDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/api/device")
public class DeviceController extends BaseController {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    public ConfigProperties configProperties;



    @GetMapping("/getOneDeviceByDeviceNo")
    public ServerResponse getOneDeviceByDeviceNo(DeviceDto deviceDto, HttpServletRequest request) throws Exception {

        if (StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createByErrorMessage("缺少设备号");
        }

        return deviceService.getOneDeviceByDeviceNo(deviceDto);
    }

    @PostMapping("/findDeviceGoodsByDeviceNo")
    public ServerResponse findDeviceGoodsByDeviceNo(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        if (StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createByErrorMessage("缺少设备号");
        }
        return deviceService.findDeviceGoodsByDeviceNo(deviceDto);
    }

    @PostMapping("/findGroupNameByDeviceNo")
    public ServerResponse findGroupNameByDeviceNo(@RequestBody DeviceDto deviceDto, HttpServletRequest request) {
        if (StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createByErrorMessage("缺少设备号");
        }
        return deviceService.findGroupNameByDeviceNo(deviceDto);
    }

    @PostMapping("/findAisleByDeviceNo")
    public ServerResponse findAisleByDeviceNo(@RequestBody DeviceDto deviceDto) {
        if (StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createByErrorMessage("缺少设备号");
        }
        return ServerResponse.createBySuccess(deviceService.findAisleByDeviceNo(deviceDto));
    }

    @PostMapping("/findSendDetectionByDeviceNo")
    public ServerResponse findSendDetectionByDeviceNo(@RequestBody DeviceDto deviceDto, HttpServletRequest request) throws Exception {
        final MemberDto memberDto = jwtTokenUtil.getMemberFromReq(request);
        if (StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createByErrorMessage("缺少设备号");
        }
        deviceDto.setMemberId(memberDto.getId());
        return ServerResponse.createBySuccess(deviceService.findSendDetectionByDeviceNo(deviceDto));
    }



    @GetMapping("/bluetoothTestResults")
    public ServerResponse bluetoothTestResults(String protocol){
        deviceService.bluetoothTestResults(protocol);
        return ServerResponse.createBySuccess();
    }

    @PostMapping("/bluetoothReShipment")
    public ServerResponse bluetoothReShipment(@RequestBody DeviceDto deviceDto, HttpServletRequest request) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        deviceDto.setMemberId(memberFromReq.getId());
        if (StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createByErrorMessage("缺少设备号");
        }
        return deviceService.bluetoothReShipment(deviceDto);
    }

    @GetMapping("/setBatteryNumber")
    public ServerResponse setBatteryNumber(DeviceDto deviceDto, HttpServletRequest request) throws Exception {
        final MemberDto memberFromReq = jwtTokenUtil.getMemberFromReq(request);
        deviceDto.setMemberId(memberFromReq.getId());
        if (StringUtils.isEmpty(deviceDto.getDeviceNo())) {
            return ServerResponse.createByErrorMessage("缺少设备号");
        }
        return deviceService.setBatteryNumber(deviceDto);
    }
}
